package com.acegear.horizon.domain.services;

import org.springframework.stereotype.Service;

import com.acegear.horizon.domain.models.jpa.UserAuth;

/**
 * Created by wangsike on 2017/1/10.
 */
@Service
public class KongService {
    /*@Value("${horizon.kong.server}")
    private String kongServer;

    @Value("${horizon.kong.name}")
    private String kongName;

    @Value("${horizon.server}")
    private String horizonServer;

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Value("${horizon.kong.enable}")
    private Boolean enable;

    @Value("${horizon.kong.cors}")
    private String kongCors;*/


    public void kongJwt(UserAuth userAuth) {
        /*if (!enable) return;
        try {
            HttpResponse<JsonNode> response = Unirest.put(kongServer + "/consumers/" + userAuth.getUserId() + "/jwt")
                    .field("key", userAuth.getAccessToken())
                    .field("secret", jwtSecret + userAuth.getAccessToken())
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }

    public void kongDeleteJwt(UserAuth userAuth) {
       /* if (!enable) return;
        try {
            HttpResponse<JsonNode> response = Unirest
                    .get(kongServer + "/consumers/" +
                            userAuth.getUserId() +
                            "/jwt")
                    .asJson();
            JSONArray datas = response.getBody().getObject().getJSONArray("data");
            for (int i = 0; i < datas.length(); i++) {
                String id = datas.getJSONObject(i).getString("id");
                HttpResponse<JsonNode> response1 = Unirest.delete(kongServer + "/consumers/" +
                        userAuth.getUserId() +
                        "/jwt/" + id).asJson();
            }
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }

    public void kongUser(UserAuth userAuth) {
       /* if (!enable) return;
        try {
            HttpResponse<JsonNode> response = Unirest.put(kongServer + "/consumers/")
                    .field("username", userAuth.getUserId())
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }

    public Boolean userExist(UserAuth userAuth) {
        /*try {
            HttpResponse<JsonNode> response = Unirest.get(kongServer + "/consumers/" + userAuth.getUserId())
                    .asJson();
            return response.getBody().getObject().has("id");
        } catch (UnirestException e) {
            e.printStackTrace();
            return false;
        }*/
    	return true;
    }

    public void addApi(String apiName) {
        /*if (!enable) return;
        try {
            String path = apiName;
            apiName = apiName.contains("/") ? apiName.replace("/", "-") : apiName;
            HttpResponse<JsonNode> response = Unirest.put(kongServer + "/apis/")
                    .field("name", "horizon-" + apiName)
                    .field("upstream_url", horizonServer + "/" + path)
                    .field("request_path", "/" + path)
                    .field("strip_request_path", true)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }

    public void addCors(String apiName) {
        /*if (!enable) return;
        try {
            String path = apiName;
            apiName = apiName.contains("/") ? apiName.replace("/", "-") : apiName;
            HttpResponse<JsonNode> response = Unirest.post(kongServer + "/apis/horizon-" + apiName + "/plugins")
                    .field("name", "cors")
                    .field("config.origin", kongCors)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }


    public void deleteApi(String apiName) {
        /*if (!enable) return;
        try {
            apiName = apiName.contains("/") ? apiName.replace("/", "-") : apiName;
            HttpResponse<JsonNode> response = Unirest.delete(kongServer + "/apis/horizon-" + apiName)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }

    public void addApiJwt(String apiName) {
        /*if (!enable) return;
        try {
            apiName = apiName.contains("/") ? apiName.replace("/", "-") : apiName;
            HttpResponse<JsonNode> response = Unirest.put(kongServer + "/apis/horizon-" + apiName + "/plugins")
                    .field("name", "jwt")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }*/
    }

    public Boolean apiExist(String apiName) {
        /*try {
            apiName = apiName.contains("/") ? apiName.replace("/", "-") : apiName;
            HttpResponse<JsonNode> response = Unirest.get(kongServer + "/apis/horizon-" + apiName)
                    .asJson();
            return response.getBody().getObject().has("id");
        } catch (UnirestException e) {
            e.printStackTrace();
            return false;
        }*/
    	return true;
    }
}
